package com.ssyc.student.adapter;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.ZzHorizontalProgressBar;
import com.ssyc.student.R;
import com.ssyc.student.bean.AchievementRunningInfo;
import com.ssyc.student.bean.ResultInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 学生端--成就--每周成就列表适配器
 * Created by Administrator on 2018/4/11 0011.
 */

public class StAchievementGetAdapter extends BaseQuickAdapter<AchievementRunningInfo.Data2Bean, BaseViewHolder> {
    private Context context;
    private Activity activity;
    private ObjectAnimator rotationAnim;

    public StAchievementGetAdapter(Activity activity, Context context, int layoutResId, @Nullable List<AchievementRunningInfo.Data2Bean> data) {
        super(layoutResId, data);
        this.context = context;
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, AchievementRunningInfo.Data2Bean item) {
        //图标
        ImageView iv = helper.getView(R.id.iv);
        if ("初见成效".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_cjcx);
        } else if ("初战告捷".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_czgj);
        } else if ("持之以恒".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_czyh);
        } else if ("势如破竹".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_srpz);
        } else if ("终成大器".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_zcdq);
        } else if ("战无不胜".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_zwbs);
        } else if ("旗开得胜".equals(item.getHonor())) {
            iv.setImageResource(R.drawable.st_qkds);
        }


        //完成任务数量
        TextView tvNum = helper.getView(R.id.tv_num);
        tvNum.setText(item.getContent());

        //进度条
        ZzHorizontalProgressBar pb = helper.getView(R.id.pb);
        pb.setBgColor(context.getResources().getColor(R.color.st_pb_hasget_pb));
        pb.setProgressColor(context.getResources().getColor(R.color.st_pb_hasget_pb));


        //没领取奖励时需要显示的包含金币和奖励以及Button的布局,需要根据状态来进行显示和yinc
        LinearLayout llNoGold = helper.getView(R.id.ll_no_gold);

        //领取奖励后需要显示的对钩
        ImageView ivHasGold = helper.getView(R.id.iv_has_gold);

        if (item.getStatus() == 0) {
            //未领取
            llNoGold.setVisibility(View.VISIBLE);
            ivHasGold.setVisibility(View.GONE);
        } else if (item.getStatus() == 1) {
            // 已经领取
            llNoGold.setVisibility(View.GONE);
            ivHasGold.setVisibility(View.VISIBLE);
        }

        //奖励金币数
        TextView tvGold = helper.getView(R.id.tv_gold);
        tvGold.setText(item.getCoin() + "金币");

        //状态
        Button btState = helper.getView(R.id.bt_state);
        btState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //领取金币的操作
                showGetPop(item);
            }
        });

    }

    /**
     * 弹出领取个人成就的弹框
     *
     * @param info
     */
    private void showGetPop(AchievementRunningInfo.Data2Bean info) {

        PopUpManager.showPop(context, R.layout.student_pop_getgold, 0.3f, activity, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //金币
                TextView tvGold = view.findViewById(R.id.tv_gold);
                tvGold.setText("成功获得" + info.getCoin() + "金币");

                //图片
                ImageView iv_inner = view.findViewById(R.id.iv_inner);
                //对整体外层的背景做帧动画处理
                //旋转动画
                rotationAnim = ObjectAnimator.ofFloat(iv_inner, "rotation", 0f, 360f);
                rotationAnim.setDuration(2500);
                rotationAnim.setRepeatCount(-1);
                rotationAnim.start();
                //经验
                TextView tvExperience = view.findViewById(R.id.tv_experience);
                tvExperience.setText("," + info.getExp() + "经验");
                Button btCommit = view.findViewById(R.id.bt_ok);
                btCommit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        http(info);
                    }
                });
            }
        });
    }

    /**
     * 请求服务器领取成就的奖励
     *
     * @param info
     */
    private void http(AchievementRunningInfo.Data2Bean info) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "9");
        map.put("platform", "8");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", "0");
        map.put("apptoken", AccountUtils.getToken(context));
        map.put("exp", info.getExp() + "");
        map.put("coin", info.getCoin() + "");
        map.put("subtype", info.getSub_type() + "");
        map.put("id", info.getSort_id() + "");


        HttpUtils.post(HttpAdress.GETACHIEVEMENT, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rotationAnim != null && rotationAnim.isRunning()) {
                    rotationAnim.cancel();
                    rotationAnim = null;
                }
                PopUpManager.dismiss();
                UiUtils.Toast("领取失败,网络异常", false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rotationAnim != null && rotationAnim.isRunning()) {
                    rotationAnim.cancel();
                    rotationAnim = null;
                }
                PopUpManager.dismiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo result = null;
                    try {
                        result = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "数据解析异常");
                    }
                    if (null != result) {
                        if ("200".equals(result.state)) {
                            //领取成功
                            info.setStatus(1);
                            notifyDataSetChanged();
                            UiUtils.Toast("领取成功", false);
                        } else {
                            //领取失败
                            UiUtils.Toast("领取失败,服务器异常" + result.state, false);
                            Log.i("test", "错误码是:" + result.state);
                        }

                    } else {
                        UiUtils.Toast("领取失败,服务器异常", false);
                    }
                }
            }
        });
    }

}
